<template>
	<view>
		<view class="head-cons" :style="'height:'+headHeight+'rpx;'">
			<view class="head-boxs">
				<image :src="common.appUrl.oss+common.imgList.sc11" mode="widthFix" @click="goBack"></image>
				<view>{{detail.title}}</view>
			</view>
		</view>
		<image :src="detail.image" mode="widthFix" class="head-img"></image>
		<view class="whites">
			<view class="moeny"><text>￥</text>{{detail.price}}</view>
			<view class="details">
				<view class="lc">
					<view class="l-title">{{detail.title}}</view>
					<view class="l-name">月售 {{detail.sales}} 好评 {{detail.praise}}</view>
				</view>
				<view class="add-cart" @click="showSelect(detail)">+加入购物车</view>
			</view>
		</view>
		<view class="tab">
			<view :class="state==1?'select-tab-name':'tab-name'" @click="tab(1)">详情</view>
			<view :class="state==2?'select-tab-name':'tab-name'" @click="tab(2)">评价</view>
		</view>
		<block v-if="state==1">
			<view class="box-con">
				<view class="c-title">商品详情</view>
				<view class="c-deta">
					<rich-text :nodes="detail.content"></rich-text>
				</view>
			</view>
		</block>
		<block v-if="state==2">
			<view class="box-con">
				<view class="c-title">商品评价</view>
				<view class="more-list" v-for="(item,index) in commentList">
					<view class="more-user">
						<image :src="item.user.avatar" mode="aspectFill" class="user-img">
						</image>
						<view class="user-detail">
							<view class="user-infor">
								<view>{{item.user.nickname}}</view>
								<image :src="common.appUrl.oss+common.imgList.sc4" v-if="item.user.is_vip==1"
									mode="widthFix"></image>
							</view>
							<view class="time">{{item.createtime}}</view>
						</view>
					</view>
					<view class="dudu">
						<view>满意度</view>
						<image
							:src="item.score >= 1 ?common.appUrl.oss+common.imgList.sc5:common.appUrl.oss+common.imgList.sc6"
							mode="widthFix"></image>
						<image
							:src="item.score >= 2 ?common.appUrl.oss+common.imgList.sc5:common.appUrl.oss+common.imgList.sc6"
							mode="widthFix"></image>
						<image
							:src="item.score >= 3 ?common.appUrl.oss+common.imgList.sc5:common.appUrl.oss+common.imgList.sc6"
							mode="widthFix"></image>
						<image
							:src="item.score >= 4 ?common.appUrl.oss+common.imgList.sc5:common.appUrl.oss+common.imgList.sc6"
							mode="widthFix"></image>
						<image
							:src="item.score >= 5 ?common.appUrl.oss+common.imgList.sc5:common.appUrl.oss+common.imgList.sc6"
							mode="widthFix"></image>
						<view>味道{{item.score_describe}}星 包装{{item.score_deliver}}星 骑士{{item.score_logistics}}星</view>
					</view>
					<view class="conswor">{{item.content}}</view>
					<view class="sp-imgs" v-if="item.images.length>0">
						<image :src="img" mode="aspectFill" v-for="(img,num) in item.images">
						</image>
					</view>
					<view class="shop-hf" v-if="item.reply">
						<view class="s-title">商家回复</view>
						<view class="s-words">{{item.reply}}</view>
					</view>
					<view class="tj-box">
						<view class="tj-title">TA的推荐：</view>
						<view class="tj-shop">
							<view>{{item.goods_name}}</view>
							<image :src="common.appUrl.oss+common.imgList.sc8" mode="widthFix"></image>
						</view>
					</view>
				</view>
			</view>
		</block>
		<view class="null"></view>
		<view class="foot-box">
			<view class="foot-detail">
				<view class="foot-cart" @click="showCart">
					<image src="/static/images/cart.png" mode="widthFix"></image>
					<view>{{cart.total_num||0}}</view>
				</view>
				<view class="foot-yc">
					<view class="foot-money">￥{{cart.total_price||'0.00'}}</view>
					<view class="foot-price">另需配送费约￥{{cart.peisong||'0.00'}}</view>
				</view>
			</view>
			<view class="pay" @click="settlement">去结算</view>
		</view>
		<view class="black" v-if="show" @click="hideCart">
			<view class="white" @click.stop="">
				<view class="white-heads">
					<view class="w-lcon">
						<view class="w-title">已选商品</view>
						<view class="w-price">打包费<text>￥{{cart.dabao}}</text></view>
					</view>
					<view class="w-rcon" @click.stop="delCart">
						<image :src="common.appUrl.oss+common.imgList.sc9" mode="widthFix"></image>
						<view>清空</view>
					</view>
				</view>
				<scroll-view scroll-y="true" class="goods-con">
					<view class="dish-detail" v-for="(item,index) in cart.list">
						<image :src="item.image" mode="aspectFill" class="dish-img"></image>
						<view class="dish-con">
							<view class="dish-more">
								<view class="dish-name">{{item.name}}</view>
								<view class="dish-num">{{item.difference}}</view>
							</view>
							<view class="dish-foot">
								<view class="dish-price"><text>￥</text>{{item.price}}</view>
								<view class="dish-edit">
									<image src="/static/images/jian.png" mode="widthFix"
										@click.stop="subtraction(item,index)"></image>
									<view>{{item.number}}</view>
									<image src="/static/images/jia.png" mode="widthFix"
										@click.stop="addition(item,index)">
									</image>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
				<view class="foot-boxss">
					<view class="foot-detail">
						<view class="foot-cart" @click="showCart">
							<image src="/static/images/cart.png" mode="widthFix"></image>
							<view>{{cart.total_num}}</view>
						</view>
						<view class="foot-yc">
							<view class="foot-money">￥{{cart.total_price||'0.00'}}</view>
							<view class="foot-price">另需配送费约￥{{cart.peisong||'0.00'}}</view>
						</view>
					</view>
					<view class="pay" @click="settlement">去结算</view>
				</view>
			</view>
		</view>
		<view class="black" v-if="tp" @click="hideSelect">
			<view class="white" @click.stop="">
				<view class="w-dish-detail">
					<image :src="goods.image" mode="aspectFill" class="w-dish-img"></image>
					<view class="w-dish-con">
						<view class="w-dish-name">{{goods.title}}</view>
						<view class="w-dish-num">{{skunames}}</view>
						<view class="w-dish-price"><text>￥</text>{{goods.price}}</view>
					</view>
				</view>
				<block v-for="(item,index) in goods.spu" :key='index'>
					<view class="sku-title">{{item.name}}</view>
					<view class="sku-box">
						<view v-for="(goods,num) in item.item" :key='num'
							:class="selects[index] === num ? 'selectcolor sku-name' :'sku-name'"
							@click.stop="handleSelectSku(index,num)">{{goods}}</view>
					</view>
				</block>
				<view class="number-box">
					<view class="num-tname">数量</view>
					<view class="dish-edit">
						<image src="/static/images/jian.png" mode="widthFix" @click.stop="decrease"></image>
						<view>{{goods.number}}</view>
						<image src="/static/images/jia.png" mode="widthFix" @click.stop="increase"></image>
					</view>
				</view>
				<view class="okkk" @click.stop="see">选好了</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
	} from 'vuex';
	export default {
		computed: {
			...mapState(['common'])
		},
		data() {
			return {
				state: 1,
				type: 0,
				cnum: 0,
				bnum: 0,
				show: false,
				tp: false,
				headHeight: 0,
				app: getApp().globalData,
				detail: {},
				page: 1,
				gid: '',
				id: '',
				commentList: [],
				cart: [],
				goods: {
					number: 1
				},
				selects: [],
			}
		},
		onLoad(e) {
			this.headHeight = (uni.getSystemInfoSync().statusBarHeight + 45) * 2
			this.gid = e.gid
			this.id = e.id
			this.getComment();
			this.getCartList();
		},
		onReachBottom() {
			this.page += 1;
			this.getComment();
		},
		methods: {
			getComment() {
				this.app.post('/api/chef/index/store_goods_info', {
					goods_id: this.gid,
					page: this.page
				}).then(res => {
					console.log(res)
					this.detail = res
					this.commentList = this.commentList.concat(res.comment.data)
				})
			},
			goBack() {
				uni.navigateBack({
					delta: 1
				})
			},
			delCart() {
				this.app.post('/api/chef/index/empty_cart', {
					shop_id: this.id,
				}).then(res => {
					console.log(res)
					this.show = false
					this.getGoods()
					this.getCartList()
				})
			},
			addition(item, index) {
				console.log(item)
				this.app.post('/api/chef/index/update_cart', {
					shop_id: this.id,
					goods_id: item.goods_id,
					sku_id: item.sku_id,
					number: item.number + 1
				}).then(res => {
					console.log(res)
					this.getCartList()
				})
			},
			subtraction(item, index) {
				console.log(item)
				this.app.post('/api/chef/index/update_cart', {
					shop_id: this.id,
					goods_id: item.goods_id,
					sku_id: item.sku_id,
					number: item.number - 1
				}).then(res => {
					console.log(res)
					this.getCartList()
				})
			},
			increase() {
				this.goods.number += 1;
				console.log(this.goods.number)
				this.$forceUpdate()
			},
			decrease() {
				if (this.goods.number > 1) {
					this.goods.number -= 1; // 数量减 1，但最小值为 1
				}
			},
			handleSelectSku(pid, id) {
				console.log(this.selects)
				this.$set(this.selects, pid, id)
				this.skunames = this.getSelectedSpecs()
			},
			see() {
				console.log(this.goods.spu.length)
				console.log(this.selects.length)
				if (this.goods.spu.length != this.selects.length) {
					uni.showToast({
						title: '请选择菜品规格',
						icon: "none"
					})
					return
				}
				var skuid = this.filterItemsByDifference(this.getSelectedSpecs())
				this.app.post('/api/chef/index/add_cart', {
					shop_id: this.id,
					goods_id: this.goods.id,
					sku_id: skuid[0],
					num: this.goods.number
				}).then(res => {
					console.log(res)
					this.tp = false
					this.getGoods()
					this.getCartList()
				})
			},
			getSelectedSpecs() {
				const selectedNames = this.selects.map((selectedIndex, groupIndex) => {
					const group = this.goods.spu[groupIndex];
					if (group && group.item && group.item[selectedIndex] !== undefined) {
						return group.item[selectedIndex];
					}
					return '';
				});

				return selectedNames.filter(name => name).join(',');
			},

			filterItemsByDifference(targetDifference) {
				const filteredItems = this.goods.sku.filter(item => item.difference === targetDifference);
				return filteredItems.map(item => item.id);
			},
			getGoods() {
				return
				this.app.post('/api/chef/index/store_goods', {
					shop_id: this.id,
					cate_id: this.typeId,
				}).then(res => {
					console.log(res)
					this.list = res
				})
			},
			getCartList() {
				this.app.post('/api/chef/index/cart_list', {
					shop_id: this.id,
				}).then(res => {
					console.log(res)
					this.cart = res
				})
			},




			tab(state) {
				this.state = state
			},
			choose(id) {
				this.typeId = id
				this.list = []
				this.getGoods()
			},
			snum(num) {
				this.cnum = num
				this.tag = num
				this.page = 1
				this.commentList = []
				this.getComment()
			},
			jnum(num) {
				this.names = num
				this.tag = num
				this.page = 1
				this.commentList = []
				this.getComment()
			},
			showCart() {
				if (this.cart.length == 0) {
					return
				}
				this.show = true
			},
			hideCart() {
				this.show = false
			},
			prompt() {
				uni.showToast({
					title: '请在购物车减购',
					icon: "none"
				})
				setTimeout(() => {
					this.show = true
				}, 1500)
			},
			showSelect(item) {
				console.log(item)
				this.tp = true
				item.number = 1
				this.goods = item
			},
			hideSelect() {
				this.tp = false
			},
			shopdetail(id) {
				uni.navigateTo({
					url: '/pages/sc/shopDetail/shopDetail?gid=' + id + '&id=' + this.id
				})
			},
			settlement() {
				if (this.cart.list.length == 0) {
					uni.showToast({
						title: '请选择商品',
						icon: "none"
					})
					return
				}
				uni.navigateTo({
					url: '/pages/sc/settlement/settlement?id=' + this.id
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F7F7F7;
	}

	.head-cons {
		width: 750rpx;
		position: fixed;
		z-index: 9999;

		.head-boxs {
			width: 750rpx;
			height: 100rpx;
			position: absolute;
			bottom: 0;
			display: flex;
			align-items: center;

			image {
				width: 34rpx;
				height: 34rpx;
				margin-left: 20rpx;
			}

			view {
				width: 80%;
				text-align: center;
				color: #FFFFFF;
				position: absolute;
				left: 10%;
			}
		}
	}

	.head-img {
		width: 750rpx;
	}

	.whites {
		width: 690rpx;
		border-radius: 20rpx;
		background: #FFFFFF;
		padding: 30rpx 0;
		margin-left: 30rpx;
		margin-top: -120rpx;
		position: relative;

		.moeny {
			margin-left: 30rpx;
			font-size: 52rpx;
			color: #FF0000;

			text {
				font-size: 30rpx;
			}
		}

		.details {
			width: 630rpx;
			margin-left: 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.lc {
				.l-title {
					font-size: 36rpx;
					color: #333333;
				}

				.l-name {
					font-size: 30rpx;
					color: #999999;
					margin-top: 20rpx;
				}
			}

			.add-cart {
				font-size: 26rpx;
				color: #FFFFFF;
				width: 172rpx;
				height: 64rpx;
				background: #84BD00;
				border-radius: 10rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}

	.tab {
		width: 750rpx;
		height: 126rpx;
		display: flex;
		align-items: center;

		.tab-name {
			color: #333333;
			font-size: 34rpx;
			border-bottom: 2px solid #F7F7F7;
			margin-left: 30rpx;
			margin-right: 30rpx;
			line-height: 60rpx;
		}

		.select-tab-name {
			color: #333333;
			font-size: 34rpx;
			border-bottom: 2px solid #84BD00;
			margin-left: 30rpx;
			margin-right: 30rpx;
			line-height: 60rpx;
		}
	}

	.box-con {
		width: 690rpx;
		background-color: #FFFFFF;
		border-radius: 20rpx;
		margin-left: 30rpx;
		padding: 30rpx 0;

		.c-title {
			margin-left: 30rpx;
			font-size: 34rpx;
			padding-bottom: 30rpx;
		}

		.c-deta {
			width: 630rpx;
			margin-left: 30rpx;
		}

		.mores {
			width: 630rpx;
			margin-left: 30rpx;
			display: flex;
			align-items: center;
			margin-top: 30rpx;

			.mo-title {
				width: 200rpx;
				font-size: 30rpx;
				color: #999999;
			}

			.mo-name {
				width: 430rpx;
				font-size: 30rpx;
				color: #333333;
			}
		}
	}

	.more-list {
		width: 630rpx;
		margin-left: 30rpx;
		border-bottom: 1px solid #D8D8D8;
		padding: 30rpx 0;

		.more-user {
			display: flex;
			align-items: center;

			.user-img {
				width: 62rpx;
				height: 62rpx;
			}

			.user-detail {
				margin-left: 17rpx;

				.user-infor {
					display: flex;
					align-items: center;

					view {
						font-size: 24rpx;
						color: #333333;
					}

					image {
						width: 73rpx;
						height: 30rpx;
						margin-left: 10rpx;
					}
				}

				.time {
					font-size: 22rpx;
					color: #999999;
				}
			}
		}

		.dudu {
			width: 630rpx;
			display: flex;
			align-items: center;
			margin-top: 20rpx;

			view {
				font-size: 24rpx;
				color: #9E9E9E;
			}

			image {
				width: 20rpx;
				height: 20rpx;
				margin-left: 6rpx;
				margin-right: 6rpx;
			}
		}

		.conswor {
			width: 630rpx;
			font-size: 30rpx;
			color: #333333;
			margin-top: 20rpx;
		}

		.sp-imgs {
			width: 630rpx;
			display: flex;
			flex-wrap: wrap;

			image {
				width: 220rpx;
				height: 220rpx;
				margin-top: 20rpx;
				margin-right: 20rpx;
			}
		}

		.shop-hf {
			width: 630rpx;
			padding: 30rpx;
			background-color: #F6F6F6;
			border-radius: 16rpx;
			margin-top: 20rpx;

			.s-title {
				font-size: 24rpx;
				color: #9E9E9E;
			}

			.s-words {
				font-size: 24rpx;
				color: #9E9E9E;
				margin-top: 20rpx;
			}
		}

		.tj-box {
			width: 630rpx;
			display: flex;
			align-items: center;
			margin-top: 20rpx;

			.tj-title {
				font-size: 24rpx;
				color: #333333;
			}

			.tj-shop {
				display: flex;
				align-items: center;
				background: rgba(140, 47, 82, 0.1);
				padding: 10rpx 20rpx;
				border-radius: 10rpx;

				view {
					font-size: 26rpx;
					color: #84BD00;
				}

				image {
					width: 20rpx;
					height: 20rpx;
					margin-top: 4rpx;
					margin-left: 4rpx;
				}
			}
		}
	}

	.foot-box {
		width: 750rpx;
		height: 120rpx;
		position: fixed;
		bottom: 0;
		background-color: #FFFFFF;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.foot-detail {
			display: flex;
			align-items: center;
			margin-left: 30rpx;

			.foot-cart {
				position: relative;
				width: 76rpx;
				height: 76rpx;

				image {
					width: 76rpx;
					height: 76rpx;
				}

				view {
					position: absolute;
					width: 30rpx;
					height: 30rpx;
					background-color: #F00;
					display: flex;
					justify-content: center;
					align-items: center;
					color: #FFFFFF;
					font-size: 22rpx;
					border-radius: 50%;
					position: absolute;
					top: 0;
					right: 0;
				}
			}

			.foot-yc {
				margin-left: 40rpx;

				.foot-money {
					font-size: 36rpx;
					color: #333333;
				}

				.foot-price {
					font-size: 24rpx;
					color: #999999;
				}
			}
		}

		.pay {
			width: 268rpx;
			height: 80rpx;
			border-radius: 10rpx;
			background-color: #84BD00;
			margin-right: 30rpx;
			font-size: 30rpx;
			color: #FFFFFF;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}

	.null {
		width: 750rpx;
		height: 150rpx;
	}

	.black {
		width: 750rpx;
		height: 100%;
		position: fixed;
		top: 0;
		z-index: 666;
		left: 0;
		background: rgba(0, 0, 0, 0.7);

		.white {
			width: 750rpx;
			position: fixed;
			bottom: 0;
			border-radius: 30px 30px 0px 0px;
			background-color: #FFFFFF;

			.w-dish-detail {
				width: 690rpx;
				margin-left: 30rpx;
				display: flex;
				align-items: center;
				padding-top: 30rpx;

				image {
					width: 150rpx;
					height: 150rpx;
				}

				.w-dish-con {
					margin-left: 28rpx;

					.w-dish-name {
						font-size: 30rpx;
						color: #333333;
					}

					.w-dish-num {
						font-size: 24rpx;
						color: #999999;
						margin-top: 10rpx;
					}

					.w-dish-price {
						font-size: 36rpx;
						color: #333333;

						text {
							font-size: 26rpx;
							color: #333333;
						}
					}
				}
			}

			.sku-title {
				width: 690rpx;
				margin-left: 30rpx;
				font-size: 30rpx;
				color: #333333;
				padding-top: 30rpx;
				padding-bottom: 10rpx;
			}

			.sku-box {
				width: 690rpx;
				margin-left: 30rpx;
				display: flex;
				flex-wrap: wrap;

				.sku-name {
					width: 216rpx;
					height: 60rpx;
					border-radius: 10rpx;
					background: #F3F3F3;
					display: flex;
					justify-content: center;
					align-items: center;
					margin-right: 20rpx;
					margin-top: 20rpx;
				}

				.selectcolor {
					border: 1px solid #84BD00;
					color: #84BD00;
					background: rgba(140, 47, 82, 0.1);
				}

				.sku-name:nth-child(3) {
					margin-right: 0;
				}

				.sku-name:nth-child(6) {
					margin-right: 0;
				}

				.sku-name:nth-child(9) {
					margin-right: 0;
				}

				.sku-name:nth-child(12) {
					margin-right: 0;
				}
			}

			.number-box {
				width: 690rpx;
				margin-left: 30rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-top: 60rpx;

				.num-tname {
					font-size: 30rpx;
					color: #333333;
				}

				.dish-edit {
					display: flex;
					align-items: center;

					image {
						width: 48rpx;
						height: 48rpx;
					}

					view {
						font-size: 30rpx;
						color: #333333;
						padding: 0 20rpx;
					}
				}
			}

			.okkk {
				width: 690rpx;
				height: 80rpx;
				border-radius: 10rpx;
				margin-top: 100rpx;
				margin-bottom: 30rpx;
				margin-left: 30rpx;
				background-color: #84BD00;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 30rpx;
				color: #FFFFFF;
			}

			.white-heads {
				width: 690rpx;
				height: 100rpx;
				margin-left: 30rpx;
				border-bottom: 1px solid #D8D8D8;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.w-lcon {
					display: flex;
					align-items: center;

					.w-title {
						font-size: 30rpx;
						color: #333333;
						margin-right: 26rpx;
					}

					.w-price {
						font-size: 26rpx;
						color: #999999;

						text {
							color: #FF0000;
						}
					}
				}

				.w-rcon {
					display: flex;
					align-items: center;

					image {
						width: 30rpx;
						height: 30rpx;
					}

					view {
						font-size: 26rpx;
						color: #999999;
						margin-left: 7rpx;
					}
				}
			}

			.goods-con {
				width: 690rpx;
				margin-left: 30rpx;
				height: 600rpx;
				margin-bottom: 30rpx;

				.dish-detail {
					width: 690rpx;
					height: 182rpx;
					display: flex;
					margin-top: 30rpx;

					.dish-img {
						width: 182rpx;
						height: 182rpx;
					}

					.dish-con {
						width: 480rpx;
						display: flex;
						align-content: space-between;
						flex-wrap: wrap;
						margin-left: 28rpx;

						.dish-more {
							width: 480rpx;

							.dish-name {
								font-size: 30rpx;
								color: #333333;
							}

							.dish-num {
								font-size: 24rpx;
								color: #999999;
								margin-top: 10rpx;
							}
						}

						.dish-foot {
							width: 480rpx;
							display: flex;
							align-items: center;
							justify-content: space-between;

							.dish-price {
								font-size: 36rpx;
								color: #333333;

								text {
									font-size: 26rpx;
									color: #333333;
								}
							}

							.dish-edit {
								display: flex;
								align-items: center;

								image {
									width: 48rpx;
									height: 48rpx;
								}

								view {
									font-size: 30rpx;
									color: #333333;
									padding: 0 20rpx;
								}
							}
						}
					}
				}
			}

			.foot-boxss {
				width: 750rpx;
				height: 7vh;
				background-color: #FFFFFF;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.foot-detail {
					display: flex;
					align-items: center;
					margin-left: 30rpx;

					.foot-cart {
						position: relative;
						width: 76rpx;
						height: 76rpx;

						image {
							width: 76rpx;
							height: 76rpx;
						}

						view {
							position: absolute;
							width: 30rpx;
							height: 30rpx;
							background-color: #F00;
							display: flex;
							justify-content: center;
							align-items: center;
							color: #FFFFFF;
							font-size: 22rpx;
							border-radius: 50%;
							position: absolute;
							top: 0;
							right: 0;
						}
					}

					.foot-yc {
						margin-left: 40rpx;

						.foot-money {
							font-size: 36rpx;
							color: #333333;
						}

						.foot-price {
							font-size: 24rpx;
							color: #999999;
						}
					}
				}

				.pay {
					width: 268rpx;
					height: 80rpx;
					border-radius: 10rpx;
					background-color: #84BD00;
					margin-right: 30rpx;
					font-size: 30rpx;
					color: #FFFFFF;
					display: flex;
					justify-content: center;
					align-items: center;
				}
			}
		}
	}
</style>